package com.qk.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Role;
import com.qk.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/roles")
@Slf4j
public class RoleController {
    @Autowired
    private RoleService roleService;
    @GetMapping
    public Result page(String name, String label, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize)   {
        log.info("查询角色列表: name = {}, label = {}, page = {}, pageSize = {}",
                name,label,page,pageSize);
                PageResult<Role> list = roleService.list(name, label, page, pageSize);
        return  Result.success(list);
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        log.info("删除角色: {}",id);
        roleService.delete(id);
        return Result.success();
    }
    @PostMapping
    public Result add(@RequestBody Role role) {
        log.info("添加角色: {}",role);
        roleService.add(role);
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("查询角色: {}",id);
        Role role = roleService.getById(id);
        return Result.success(role);
    }
    @PutMapping
    public Result update(@RequestBody Role role) {
        log.info("修改角色: {}",role);
        roleService.update(role);
        return Result.success();
    }
    @GetMapping("/list")
    public Result list(String name, String label, LocalDateTime createTime, LocalDateTime updateTime) {
        log.info("查询所有角色: name = {}, label = {}, createTime = {}, updateTime = {}",
                name,label,createTime,updateTime);
        //    PageResult<Role> list(String name, String label, Integer page, Integer pageSize);
                List<Role> list = roleService.list(name, label, createTime, updateTime);
                return Result.success(list);

    }
}
